<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="renderer" content="webkit">
	<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
	<title th:text="${projectName}+'首页'"></title>
	<!--[if lt IE 9]>
	<meta http-equiv="refresh" content="0;ie.html"/>
	<![endif]-->
	<link th:href="@{/favicon.ico}" rel="stylesheet"/>
	<link th:href="@{/js/layuiadmin-1.2.1/layui/css/layui.css}" rel="stylesheet"/>
	<link th:href="@{/js/layuiadmin-1.2.1/style/admin.css}" rel="stylesheet"/>
	<link th:href="@{/dhecp/css/dh-index.css?v=17}" rel="stylesheet"/>
	<link th:href="@{/dhecp/css/dh-lockscreen.css?v=1}" rel="stylesheet"/>
	<link th:href="@{/dhecp/css/dh-icon.css?v=3}" rel="stylesheet"/>
	<link th:href="@{/js/plugins/tips/tips.css}" rel="stylesheet"/>
	<link th:href="@{/dhecp/css/dh-motif.css?v=2}" rel="stylesheet" title="motif"/>
	<script th:src="@{/dhecp/js/dh-motif.js?}"></script>
	<script th:src="@{/dhecp/js/dh-motif-list.js}"></script>
	<script th:src="@{/js/jquery/jquery.min.js}"></script>
	<script th:src="@{/dhecp/js/dh-ui.js}"></script>
	<style>
		.footer-view{
			box-sizing: border-box;
			width: 70px;
			text-align: center;
			line-height: 70px;
			/* 关键代码 */
			height:80px;
			/* 底部固定关键代码 */
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 999;
			margin-left: 70px;
		}
		.footer-view-bi{
			box-sizing: border-box;
			width: 70px;
			text-align: center;
			line-height: 70px;
			/* 关键代码 */
			height:80px;
			/* 底部固定关键代码 */
			position: fixed;
			bottom: 0;
			left: 80px;
			z-index: 999;
			margin-left: 70px;
		}
		.footer{
			border: none;
			border-radius: 15px;
			background-color: #27888F;
			opacity: 0.6;
			color: white;
			cursor: pointer;
			width: 70px;
			height: 70px;
			margin: 0 auto;
			font-weight: 600;
			font-size: 20px;
			box-shadow: #D2D8E0 2px 2px 2px 2px ;
		}
		.footer-bi{
			border: none;
			border-radius: 15px;
			background-color: #e7668a;
			opacity: 0.6;
			color: white;
			cursor: pointer;
			width: 70px;
			height: 70px;
			margin: 0 auto;
			font-weight: 600;
			font-size: 20px;
			box-shadow: #D2D8E0 2px 2px 2px 2px ;
		}
		.eKnowAiBtn{
			opacity: 1;
		}
	</style>
</head>
<body class="layui-layout-body">
<div id="LAY_app">
	<div id="MenBan" style="z-index: 999;height: 100vh;width: 100vw;position: absolute;display: none" ></div>
	<div class="layui-layout layui-layout-admin">
		<div class="layui-header">
			<!-- 头部区域 -->
			<th:block th:include="indexinclude :: indexheader"/>
		</div>
		<!-- 侧边菜单 -->
		<div class="layui-side layui-side-menu">
			<div class="layui-side-scroll">
				<th:block th:include="indexinclude :: indexleft"/>
			</div>
			<!--底部固定条th:if="${extraButton.addbtn}"-->
			<div class="footer-view" shiro:hasPermission="eKnowAi:Btn:list"><div class="footer" id="eKnowAi" onclick="toEknow()">逸 道</div></div>
			<!--底部固定条th:if="${extraButton.addbtn}"-->
			<div class="footer-view-bi" shiro:hasPermission="bi:view"><div class="footer-bi" id="id_bi" onclick="toBi()">B I</div></div>
		</div>


		<!-- 页面TAB标签 -->
		<th:block th:include="indexinclude :: indextab"/>
		<!-- 主体内容 -->
		<div class="layui-body" id="LAY_app_body">
			<!-- 加载动画 -->
			<div class="page-loading">
				<!-- 				<div class="ball-loader"> -->
				<div class="signal-loader">
					<span></span>
					<span></span>
					<span></span>
					<span></span>
				</div>
			</div>
			<!-- tab页内容 -->
			<div class="layadmin-tabsbody-item layui-show">
				<iframe th:src="@{/system/main}" frameborder="0" class="layadmin-iframe"></iframe>
			</div>


		</div>
		<!-- 辅助元素，一般用于移动设备下遮罩 -->
		<div class="layadmin-body-shade" layadmin-event="shade"></div>
	</div>
</div>

<div class="tips"></div>
<!-- 锁屏 -->
<th:block th:include="indexinclude :: lockscreen"/>
<script th:src="@{/js/layuiadmin-1.2.1/layui/layui.js}"></script>
<script th:src="@{/js/jquery/jquery.cookie.js}"></script>
<script th:src="@{/dhecp/js/md5.js}"></script>
<script th:inline="javascript">
	/*<![CDATA[*/
	//定义全局通用参数
	var session = [[${session}]],//会话数
		mrx = [],
		isUnfold = true; //默认展开
	/*]]>*/
</script>

<script type="text/javascript">

	$("#eKnowAi").mouseover(function() {
		$("#eKnowAi").addClass("eKnowAiBtn");
	});
	$("#eKnowAi").mouseout(function() {$("#eKnowAi").removeClass("eKnowAiBtn");});

	$("#id_bi").mouseover(function() {
		$("#id_bi").addClass("eKnowAiBtn");
	});
	$("#id_bi").mouseout(function() {$("#id_bi").removeClass("eKnowAiBtn");});

	layui.config({
		base: '/js/layuiadmin-1.2.1/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'dtree']);
	(function () {//加载框架页面后默认执行的方法
		refreshTask();
		refreshNotice();
		refreshMail();
		alterMotif()

		$form = $('.lock-screen-wrapper .lock-screen-form');
		$psw = $form.find('.lock-screen-psw');
		$tip = $form.find('.lock-screen-tip');
		$time = $('.lock-screen-wrapper .lock-screen-time');
		$date = $('.lock-screen-wrapper .lock-screen-date');

		if ($.cookie('dhecp_lockScreen')) {//存在锁屏cookie
			if ($.cookie('dhecp_lockScreen') != hex_hmac_md5("dhecp" + session.userId.toLowerCase(), session.userId.toLowerCase())) listenerLockScreen();//锁屏cookie与登陆时不同则锁屏
		} else window.location.href = '/login';//锁屏cookie被清空，返回登录页
	})(jQuery);
	var iframe = document.getElementById('LAY_app_tabsheader');
	// console.log("iframe",iframe)
	var $, layer, util;
	var showFlag = false;
	var $form, $psw, $tip, $time, $date;//获取锁屏的各个组件
	var moduleList = [];//功能列表
	layui.use(['layer', 'jquery', 'util'], function () {
		$ = layui.jquery, layer = layui.layer, util = layui.util;
		setDate();
		// setTimeout(function(){
		setInterval(function () {
			setDate();
		}, 1000);

	});
	$.tablelist.init({
		modalName: "修改密码"
		,createUrl: "user_resetpwd"
	});

	//默认选中的菜单
	mrx = $("#icu996").children().attr("id");
	$('#' + mrx).addClass("layui-back").siblings().removeClass("layui-back");
	//赋予 选中的div下类名为 layui-left-menu-two 的元素 样式
	$('div#' + mrx + ' .layui-left-menu-two').addClass("layui-hide");


	$(document).ready(function () {
		//只显示有菜单的分类
		isUnfold = false
		alterMotif()
		flexible()
		$("div.menusGroupId").hide()
		$("div.layui-left-menu-three").addClass("three-menu-out")
		$("div.three-menu").each(function () {
			if ($(this).children().length > 0) {
				$(this).parent('div.menusGroupId').show()
			}
		});

		// 二级菜单经过的时候显示/移开隐藏
		var lis = document.getElementsByClassName("menu-two-box")

		$.each(lis, function (index, ele) {
			lis[index].onmouseover = function () {
				$(this).addClass("two-menu-show2").siblings().removeClass("two-menu-show2")
				lis[index].getElementsByClassName("layui-left-menu-three")[0].classList.add("three-menu-ver")
				lis[index].getElementsByClassName("layui-left-menu-three")[0].classList.remove("three-menu-out")
			}

			lis[index].onmouseout = function () {
				$(this).removeClass("two-menu-show2")
				lis[index].getElementsByClassName("layui-left-menu-three")[0].classList.add("three-menu-out")
				lis[index].getElementsByClassName("layui-left-menu-three")[0].classList.remove("three-menu-ver")
			}
		})


	});

	$("div.layui-left-menu-three").bind('DOMSubtreeModified', function() {
		var selectedNum=$(this).css("display");
	});

	iframe.onload = function() {
		console.log(123)
	}




	function refreshTask() {//计时刷新待办任务
		$.get('/system/moduletask/listwcls', function(res){
			if(Number(res.data[0].taskCount) + Number(res.data[0].actCount) > 0){
				$('#todoTask').show();
				$('#todoTask').text(Number(res.data[0].taskCount) + Number(res.data[0].actCount));
				if ($("iframe[src='/flowable/task/task']").length) { //如果打开了任务中心，则影响其提示数量
					var taskContentWindow = $("iframe[src='/flowable/task/task']")[0].contentWindow;
					taskContentWindow.$('.badge1').text(taskContentWindow.$('#filterWeekly').prop("checked") ? (res.data[0].actCount - res.data[0].planCount - res.data[0].finishCount) : res.data[0].actCount); //待办流程任务数
					taskContentWindow.$('.badge2').text(res.data[0].taskCount); //未处理功能任务数
					taskContentWindow.$('.badge3').text(res.data[0].planCount + res.data[0].finishCount); //计划总结
				}
			}else $('#todoTask').hide();
		});
		setTimeout(function () {
			refreshTask();
		}, 180000);
	}

	function shops(){//逸思精品
		url = '[[${dto.urlAdmin}]]' + '/web/business/tpBoutique/index';
		url = url + '?loginName=[[${dto.userId}]]&pass=[[${dto.passwordEncryped}]]'
		window.open(url, "_blank");
	}

	function refreshNotice() {//刷新通知数量
		$.get('/system/modulenotice/listwds', function (res) {//查询未读通知数
			if (res.data[0].noticeCount > 0) {
				$('#notice').text(res.data[0].noticeCount);
				$('#notice').show();
			} else {
				$('#notice').hide();
			}
			;
		});
		setTimeout(function () {//三分钟刷新
			refreshNotice();
		}, 180000);
	}

	function refreshMail() {//刷新邮件未读数量
		$.get('/system/mail/listwds', function (res) {//查询未读通知数
			if (res.data[0].mailWdCount > 0) {
				$('#unreadMail').text(res.data[0].mailWdCount);
				$('#unreadMail').show();
			} else {
				$('#unreadMail').hide();
			}
			;
		});
		// $.get('/system/oamail/listwds',function(res){//未读收件任务数
		// 	if(res.data > 0){
		// 		$('#unreadMail').text(res.data);
		// 		$('#unreadMail').show();
		// 	}else {
		// 		$('#unreadMail').hide();
		// 	}
		// });
		setTimeout(function () {//三分钟刷新
			refreshMail();
		}, 180000);
	}

	//一级事件操作
	function hitTheMenu(e) {
		//给选择对象添加背景样式 并去除同级样式
		$('#' + e).addClass("layui-back").siblings().removeClass("layui-back")
		//清除 所有div 下的类名 为 layui-left-menu-two  的 子元素 的 样式
		$('div').children(".layui-left-menu-two").removeClass("layui-hide");
		//赋予 选中的div下类名为 layui-left-menu-two 的元素 样式
		$('div#' + e + ' .layui-left-menu-two').addClass("layui-hide").find("ul").removeClass("two-menu-show").find("div").removeClass("three-menu-show")
	}

	//点击二级菜单时 显示列表
	function hitTheTwoMenu(e) {
		//创建蒙版
		$("#MenBan").css("display","block")

		allmenuGroupID()
		$("div.menu-three-txt").children().children("li#menuall").addClass("layui-this").siblings().removeClass("layui-this")
		$('#' + e).addClass("two-menu-show").siblings().removeClass("two-menu-show")
		$('#' + e).children("div.layui-left-menu-three").addClass("three-menu-show")
		$('#' + e).siblings().children("div.layui-left-menu-three").removeClass("three-menu-show")
		isUnfold = false //展开侧边栏
		flexible()
		event.stopPropagation()
	}
	function toEknow() {
		//console.log('[[${dto.urlAdmin}]]')
		//url = '[[${dto.urlAdmin}]]' + '/eKnowAi/chatPage/aiTalk'
		url = '[[${dto.urlAdmin}]]' + '/indexCommonOfMenu?menuId=121';
		url = url + '&loginName=[[${dto.userId}]]&pass=[[${dto.passwordEncryped}]]'
		window.open(url);
	}
	function toBi() {
		//console.log('[[${dto.urlAdmin}]]')
		//url = '[[${dto.urlAdmin}]]' + '/eKnowAi/chatPage/aiTalk'
		url = '[[${dto.urlAdmin}]]' + '/web/bi/index?a=1';
		url = url + '&loginName=[[${dto.userId}]]&pass=[[${dto.passwordEncryped}]]'
		window.open(url);
	}
	function toTask() {
		//console.log('[[${dto.urlAdmin}]]')
		//url = '[[${dto.urlAdmin}]]' + '/eKnowAi/chatPage/aiTalk'
		url = '[[${dto.urlAdmin}]]' + '/web/flow/ysp/toIndex';
		url = url + '?loginName=[[${dto.userId}]]&pass=[[${dto.passwordEncryped}]]'
		window.open(url);
	}




	function newTab(url, title, e) {
		if(url.startsWith('http')) {
			url = url + '&loginName=[[${dto.userId}]]&pass=[[${dto.passwordEncryped}]]'
			window.open(url);
		} else {
			$('#' + e).children("div.layui-left-menu-three").removeClass("three-menu-show")
			
			// 验证登录过期
			$.ajax({
				type: "POST",
				url: '/checkLogin',
				data: {},
				success: function (data) {
					if (data.code == 500) {
						layer.msg(data.msg);
						setTimeout(function(){
							window.location.reload();
						}, 1000);
					} else {
						if (top.layui.index) top.layui.index.openTabsPage(url, title);
						else window.open(url);
						event.stopPropagation();
					}
				}
			});
		}
	}

	function allmenuGroupID() {
		//过滤空的菜单组
		$("div.menusGroupId").hide()
		$("div.layui-left-menu-three").addClass("three-menu-out")
		$("div.three-menu").each(function () {
			if ($(this).children().length > 0) {
				$(this).parent('div.menusGroupId').show()
			}
		});
	}


	$("body").click(function () {//切换主题事件
		if (isMotif !== JSON.parse(window.localStorage.getItem('layuiAdmin')).theme.color.main){
			isMotif = JSON.parse(window.localStorage.getItem('layuiAdmin')).theme.color.main
			alterMotif()
		}
	})
	//点击空白关闭 三级菜单
	$(document).click(function (e) {
		// $("#MenBan").css("display","none")
		var _con = $("div.layui-left-menu-three");   // 设置目标区域
		if (!_con.is(e.target) && _con.has(e.target).length === 0) {
			$("div.layui-left-menu-three").removeClass("three-menu-show")
		}
	});

	$("#MenBan").click(function () {//蒙版事件
		$("#MenBan").css("display","none")
		$("div.layui-left-menu-three").removeClass("three-menu-show")
	})


	//选中分类时候的颜色/过滤事件
	$(".layui-tab-title li").on("click", function () {
		$(this).addClass("layui-this").siblings().removeClass("layui-this");

		let x = $(this).attr('dataset.name')
		if (x === '全部') {
			allmenuGroupID()

		} else {
			$(this).closest(".three-menu-show").children().children("div.menusGroupId").hide()
			$(this).closest(".three-menu-show").children().children("#" + x).show()
		}
		return false
	});


	//侧边伸缩 事件
	function flexible() {
		if (isUnfold) {
			//切换 图标
			$('#LAY_app_flexible').removeClass("layui-icon-shrink-right").addClass("layui-icon-spread-left");
			$('.layadmin-pagetabs').animate({"left": "113px"});
			$('.layui-body').animate({"left": "113px"});
			$('.layui-left-menu-tit').animate({"width":"50px"});
			$('.layui-left-menu-two').animate({"width":"50px"});
			$(".layui-layout-admin .layui-left-menu-three").css("margin-left","40px")
			$('.three-menu-show').removeClass("three-menu-show")
			$('.two-text').children("a").children("span").hide();
			$('.layui-left-menu-text').children("span").text("企");
			isUnfold = false    //切换状态
		} else {
			$('#LAY_app_flexible').removeClass("layui-icon-spread-left").addClass("layui-icon-shrink-right");
			$('.layadmin-pagetabs').animate({"left": "221px"});
			$('.layui-body').animate({"left":"221px"});
			$('.layui-left-menu-tit').animate({"width":"158px"});
			$('.layui-left-menu-two').animate({"width":"158px"},function () {
				$('.two-text').children("a").children("span").show();
			});
			$(".layui-layout-admin .layui-left-menu-three").css("margin-left","148px")
			$('.layui-left-menu-text').children("span").text("企业综合管理平台");
			isUnfold = true     //切换状态
		}
	}

	function changePassword() {//修改密码
		console.log("修改密码")
		$.modal.open($.tablelist._option.modalName, '/system/user/passwordedit?userId='+session.userId,520,630,"","20%");
	}

	function lockScreen() {//锁定屏幕
		layer.open({
			type: 1,
			title: '锁定屏幕',
			offset: '20%',//距离顶部10%
			area: ['520px', '217px'],
			btn: ['锁定', '取消'],
			shadeClose: true,//点击弹层外区域关闭
			content: '<div class="layui-form-pane" style="margin:10px 20px"><p style="color:red;text-align:center;height:30px">请输入一次性锁屏密码，用于解锁屏幕</p>' +
					'<div style="margin-left: 80px;margin-top: 10px;"><label class="layui-form-label" style="width:120px"><i style="color:#ff0000">*&nbsp;&nbsp;</i>锁屏密码</label>' +
					'<input style="width:220px" type="password" id="lockScreenPSW" class="layui-input layui-input-block" autocomplete="off"></div></div>',
			success: function (layero, index) {
				$(document).keydown(function (e) {//监听键盘动作
					if (e.keyCode === 13) {//回车触发提交
						$(".layui-layer-btn0").trigger("click");
					}
				});
				$('#lockScreenPSW').focus();
				$('#moduleSearch').attr("readonly",true);
			},
			yes: function (index) {
				if ($('#lockScreenPSW').val()) {//输入的情况下
					$.cookie('dhecp_lockScreen', hex_hmac_md5("dhecp" + session.userId.toLowerCase(), $('#lockScreenPSW').val()), {
						expires: 1,
						path: '/'
					});//记录锁屏密码,存放cookie
					layer.closeAll();
					listenerLockScreen();//锁屏后的动作监听
				} else {
					layer.tips('请输入！', '#lockScreenPSW');//提示不能为空
				}
			},
			cancel: function (index) {
				return true;
			},
			end: function () {
				$('#moduleSearch').attr("readonly",false);
				$(document).unbind();//移除事件——监听键盘动作
			}
		});
	}

	function listenerLockScreen() {//锁屏后的动作监听
		$('#lockscreen').show();//显示锁定屏幕块
		$(window).keydown(function (event) {//监听enter键
			if ($.cookie('dhecp_lockScreen') != hex_hmac_md5("dhecp" + session.userId.toLowerCase(), session.userId.toLowerCase())) {
				if (event.keyCode === 13) {
					$psw.val('');
					$tip.text('密码不正确');
				} else if (event.keyCode === 8 && !$psw.val()) {
					restForm();
					if (event.preventDefault) event.preventDefault();
					if (event.returnValue) event.returnValue = false;
				}
			}
		});
		$psw.on('input', function () {//监听输入
			if ($.cookie('dhecp_lockScreen') === hex_hmac_md5("dhecp" + session.userId.toLowerCase(), $psw.val())) {//当输入与锁屏密码相同时直接触发
				$.cookie('dhecp_lockScreen', hex_hmac_md5("dhecp" + session.userId.toLowerCase(), session.userId.toLowerCase()), {
					expires: 1,
					path: '/'
				});//记录锁屏密码,存放cookie
//         	$.removeCookie('dhecp_lockScreen',{path:'/'});//追杀cookie
				restForm();
				$('#lockscreen').hide();//隐藏锁定屏幕块
			}
		});
		$form.find('.lock-screen-enter').on('click', function (e) {// 监听按钮点击
			if (showFlag) {
				restForm();
				showFlag = false;
			} else {
				$form.addClass('show-psw show-back');
				$psw.focus();
				$psw.val('');
				showFlag = true;
			}
		});

		function restForm() {//重置
			$psw.blur();
			$psw.val('');
			$tip.text('');
			$form.removeClass('show-psw');
			$form.removeClass('show-back');
		}
	}

	function setDate() {//刷新锁屏时间
		var date = new Date();
		$time.text(util.toDateString(date, 'HH:mm:ss'));
		var weeks = ['日', '一', '二', '三', '四', '五', '六'];
		$date.text(util.toDateString(date, 'MM月dd日，星期') + weeks[date.getDay()]);
	}

	function lockModule(layid, passwordAuthentication, moduleTable, passwordPeriodMM) {//锁定功能，需要密码解锁
		layer.open({
			type: 1,
			title: '查询验证',
			offset: '20%',//距离顶部10%
			area: ['400px', '200px'],
			btn: ['解锁', '取消'],
			content: '<div class="layui-form-pane" style="margin:10px 20px"><p style="color:red;text-align:center;height:30px">请输入' +
					(passwordAuthentication == 'userpassword' ? '登陆密码' : '二级密码') + '，用于解锁功能</p>' +
					'<label class="layui-form-label" style="width:120px"><i style="color:red">*&nbsp;&nbsp;</i>解锁密码</label>' +
					'<input style="width:220px" type="password" id="pwd" class="layui-input layui-input-block" autocomplete="off"></div>',
			success: function (layero, index) {
				$(document).keydown(function (e) {//监听键盘动作
					if (e.keyCode === 13) {//回车触发提交
						$(".layui-layer-btn0").trigger("click");
					}
				});
				$('#pwd').focus();
				$('#moduleSearch').attr("readonly",true);
			},
			btn1: function (index) {//解锁按钮
				if ($('#pwd').val()) {//输入的情况下
					$.post('/common/password/matchepassword', {
						'matchType': passwordAuthentication,
						'pwd': $('#pwd').val()
					}, function (res) {
						if (res.code == 0) {//验证成功
							$.cookie('dhecp_' + moduleTable, true, {
								expires: new Date(new Date().getTime() + (1000 * 60 * Number(passwordPeriodMM))),
								path: '/'
							});//添加解锁功能cookie
							$('.page-loading').hide();
							layer.closeAll();
							layer.msg(res.msg, {icon: 1, offset: '10%', shift: 5});
						} else {//验证失败
							$('#pwd').val('');
							layer.msg(res.msg, {icon: 2, offset: '10%', shift: 6});
						}
					})
				} else {
					layer.tips('请输入！', '#pwd');//提示不能为空
				}
			},
			btn2: function (index) {//取消按钮
				$('.page-loading').hide();
				element.tabDelete('layadmin-layout-tabs', layid);//指定删除tab，防止快速点击切换其他tab页面
			},
			cancel: function (index) {//右上角关闭
				$('.page-loading').hide();
				element.tabDelete('layadmin-layout-tabs', layid);//指定删除tab，防止快速点击切换其他tab页面
			},
			end: function () {
				$('#moduleSearch').attr("readonly",false);
				$(document).unbind();//移除事件——监听键盘动作
			}
		});
	}

	function moduleSearch() {
		var ddHtml = '';
		$.each(moduleList, function (i, v) {
			$.each(v.children, function (ii, vv) {
				if (vv.title.indexOf($('#moduleSearch').val()) != -1) ddHtml += '<dd lay-unselect><a lay-href="' + vv.href + '" lay-text="' + vv.title + ' - 查询">' + vv.title + '</a></dd>';
			})
		})
		$('#moduleSearch').next().next().html(ddHtml);
	}
</script>
<script th:src="@{/dhecp/js/dh-menu.js?v=32}"></script>
<script th:src="@{/js/plugins/tips/tips.js}"></script>
<script>


	//websocket 操作
	var socket;
	function openSocket() {
		if (typeof (WebSocket) == "undefined") {
			console.log("您的浏览器不支持WebSocket");
		} else {
			console.log("您的浏览器支持WebSocket");
			//实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
			//等同于socket = new WebSocket("ws://localhost:8888/xxxx/im/25");
			//var socketUrl="${request.contextPath}/im/"+$("#userId").val();
			var socketUrl = "wss://" + window.location.host + "/imserver/" + session.userId;
			socketUrl = socketUrl.replace("https", "ws").replace("http", "ws");
			console.log(socketUrl);
			if (socket != null) {
				socket.close();
				socket = null;
			}
			socket = new WebSocket(socketUrl);
			//打开事件
			socket.onopen = function () {
				console.log("websocket已打开");
				//socket.send("这是来自客户端的消息" + location.href + new Date());
			};
			//获得消息事件
			socket.onmessage = function (msg) {
				console.log(msg.data);
				if (msg.data.indexOf("contentText") >= 0) {
					var res = JSON.parse(msg.data);
					if (typeof res == 'object' && res.toUserId == session.userId) {
						tipsindex('<div style="height: 80px;width: 240px;font-size: 16px;"><a lay-href="/system/mail" onclick="reloadMial()" lay-text="邮件系统" style="color: #fff!important;">' + res.contentText + '</a></div>')
						refreshMail();	//刷新邮件数量
					}
				}
			};
			//关闭事件
			socket.onclose = function () {
				console.log("websocket已关闭");
			};
			//发生了错误事件
			socket.onerror = function () {
				console.log("websocket发生了错误");
			}
		}
	}

	function sendMessage(toUserId, contentText) {
		if(typeof(WebSocket) == "undefined") {
			console.log("您的浏览器不支持WebSocket");
		}else {
			console.log("您的浏览器支持WebSocket");
			console.log('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
			if(socket.readyState == 0){//Socket has been created. The connection is not yet open.
				var socketInterval = setInterval(function(){//间歇器检查websocket连接状态重新发送
					if(socket.readyState == 1){//The connection is open ,can ready to communicate.
						socket.send('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
						clearInterval(socketInterval);
					}
				},1000);
			}else if(socket.readyState == 1){//The connection is open and ready to communicate.
				socket.send('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
			}else{//2：The connection is in the process of closing.	3：The connection is closed or couldn't be opened.
				console.log("WebSocket已关闭,尝试重新连接！");
				openSocket();
				var socketInterval = setInterval(function(){//间歇器检查websocket连接状态重新发送
					if(socket.readyState == 1){//The connection is open ,can ready to communicate.
						socket.send('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
						clearInterval(socketInterval);
					}
				},1000);
			}
		}
	}

	//开启socket
	openSocket();

	//邮件消息提示
	function tipsindex(content) {
		$('.tips').initTips({
			title: '邮件系统',
			duration: 10000,
			message: content,
			toastType: 'info'
		});
	}

	//邮件系统已打开时，刷新邮件系统
	function reloadMial() {
		$(".layadmin-iframe").each(function (i, v) {
			if ($(v).attr("src") == '/system/mail')
				$(v).attr('src', '/system/mail');
		})
	}
</script>
</body>
</html>